<template>
  <div class="page-header">
    <div class="page-header-index-wide">
      <s-breadcrumb />
      <div class="page-header-actions">
        <a-icon :spin="loading" @click="refreshPage" class="page-header-actions-item" type="sync" />
        <a-icon
          v-if="!fullscreen && !layout.fullscreen"
          @click="fullScreen('enter')"
          class="page-header-actions-item"
          type="fullscreen"
        />
        <a-icon
          v-else-if="!layout.fullscreen"
          @click="fullScreen('exit')"
          class="page-header-actions-item"
          type="fullscreen-exit"
        />
      </div>
    </div>
    <div class="detail" v-if="!$route.meta.hiddenHeaderContent">
      <div class="main">
        <div class="row">
          <img v-if="logo" :src="logo" class="logo" />
          <h1 v-if="title" class="title">{{ title }}</h1>
          <div class="action">
            <slot name="action"></slot>
          </div>
        </div>
        <div class="row">
          <div v-if="avatar" class="avatar">
            <a-avatar :src="avatar" />
          </div>
          <div v-if="this.$slots.content" class="headerContent">
            <slot name="content"></slot>
          </div>
          <div v-if="this.$slots.extra" class="extra">
            <slot name="extra"></slot>
          </div>
        </div>
      </div>
      <div>
        <slot name="pageMenu"></slot>
      </div>
    </div>
  </div>
</template>

<script>
import Breadcrumb from '@/components/tools/Breadcrumb';

export default {
  name: 'PageHeader',
  components: {
    's-breadcrumb': Breadcrumb,
  },
  inject: ['layout'],
  props: {
    title: [String, Boolean],
    logo: {
      type: String,
      default: '',
      required: false,
    },
    avatar: {
      type: String,
      default: '',
      required: false,
    },
    loading: {
      type: Boolean,
      default: false,
      required: false,
    },
    fullscreen: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {};
  },
  methods: {
    refreshPage() {
      this.$emit('refresh');
    },
    fullScreen(action) {
      this.$emit('fullscreen', action);
    },
  },
};
</script>

<style lang="less" scoped>
.page-header {
  background: #fff;
  padding: 16px 32px;
  border-bottom: 1px solid #e8e8e8;

  .detail {
    display: flex;

    .avatar {
      & > span {
        border-radius: 72px;
        display: block;
        width: 72px;
        height: 72px;
      }
    }

    .main {
      width: 100%;
      display: flex;

      .row {
        display: flex;
        align-items: center;
      }

      .title {
        font-size: 20px;
        font-weight: 500;
        font-size: 20px;
        line-height: 28px;
        font-weight: 500;
        color: rgba(0, 0, 0, 0.85);
        margin-bottom: 16px;
      }
      .logo {
        width: 28px;
        height: 28px;
        border-radius: 4px;
        margin-right: 16px;
      }
      .content, .headerContent {
        flex: auto;
        color: rgba(0, 0, 0, 0.45);
        line-height: 22px;

        .link {
          margin-top: 16px;
          line-height: 24px;

          a {
            font-size: 14px;
            margin-right: 32px;
          }
        }
      }
      .extra {
        flex: 0 1 auto;
        margin-left: 88px;
        min-width: 242px;
        text-align: right;
      }
      .action {
        margin-left: 56px;
        min-width: 266px;
        flex: 0 1 auto;
        text-align: right;
        &:empty {
          display: none;
        }
      }
    }
  }
  .page-header-index-wide {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .page-header-actions {
    display: flex;
    &-item {
      font-size: 16px;
      padding: 4px;
      cursor: pointer;
      &:not(:first-child) {
        margin-left: 4px;
      }
      &:hover {
        color: @primary-color;
      }
    }
  }
}

.mobile .page-header {
  .main {
    .row {
      flex-wrap: wrap;

      .avatar {
        flex: 0 1 25%;
        margin: 0 2% 8px 0;
      }

      .content,
      .headerContent {
        flex: 0 1 70%;

        .link {
          margin-top: 16px;
          line-height: 24px;

          a {
            font-size: 14px;
            margin-right: 10px;
          }
        }
      }

      .extra {
        flex: 1 1 auto;
        margin-left: 0;
        min-width: 0;
        text-align: right;
      }

      .action {
        margin-left: unset;
        min-width: 266px;
        flex: 0 1 auto;
        text-align: left;
        margin-bottom: 12px;

        &:empty {
          display: none;
        }
      }
    }
  }
}

.mobile .page-header {
  .main {
    .row {
      flex-wrap: wrap;

      .avatar {
        flex: 0 1 25%;
        margin: 0 2% 8px 0;
      }

      .content,
      .headerContent {
        flex: 0 1 70%;

        .link {
          margin-top: 16px;
          line-height: 24px;

          a {
            font-size: 14px;
            margin-right: 10px;
          }
        }
      }

      .extra {
        flex: 1 1 auto;
        margin-left: 0;
        min-width: 0;
        text-align: right;
      }

      .action {
        margin-left: unset;
        min-width: 266px;
        flex: 0 1 auto;
        text-align: left;
        margin-bottom: 12px;

        &:empty {
          display: none;
        }
      }
    }
  }
}
</style>
